<?php
class CategoriasController extends AppController {

	var $name = 'Categorias';
	var $helpers = array('Time','Html', 'Form', 'Javascript','Flash');
	
//==========================================//
	function menu($opt)
	{
		$this->Categoria->recursive=0;
		
		switch($opt)
		{
			case 'clasificados':
				$conditions = array('conditions'=>array('Categoria.activa'=>1,'Categoria.subcat_id'=>0,'Categoria.id <>'=>0,'Categoria.gratuita'=>1),'order'=>'Categoria.nombre ASC');
				break;
			case 'destacados':
			$conditions = array('conditions'=>array('Categoria.activa'=>1,'Categoria.subcat_id'=>0,'Categoria.id <>'=>0,'Categoria.gratuita'=>0),'order'=>'Categoria.nombre ASC');
				break;

		}
		$items = $this->Categoria->find('all', $conditions);
		
		if(isset($this->params['requested']))
		{
			return $items;
		}
	}



//==========================================//
	function ver_categoria($id =null)
	{
		$this->Categoria->recursive=0;
		if( !$id )
		{
			$this->Session->setFlash(__('Categoria Invalida.', true));
			$this->redirect('/');
		}
		
		$categorias = $this->Categoria->find('all', array('conditions'=>array('subcat_id'=>$id,'activa'=>1)));
		$maincat = $this->Categoria->find('first', array('fields'=>array('nombre'),'conditions'=>array('id'=>$id, 'activa'=>1)));
		$this->set('categorias' , $categorias );
		$this->set('maincat',$maincat);
	}

	

//==========================================//
	function admin_index() {
	if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
	$this->layout = "admin";
		$this->Categoria->recursive = 0;
		$this->paginate['limit'] = 35;
		$this->paginate['order'] = array('Categoria.nombre' =>  'ASC');
		$this->set('categorias', $this->paginate());
	}

//==========================================//
	function admin_view($id = null) {
			if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
	$this->layout = "admin";
		if (!$id) {
			$this->Session->setFlash(__('Categoria Invalida.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('categoria', $this->Categoria->read(null, $id));
	}

//==========================================//
	function admin_add() {
			if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
		$this->layout = "admin";
		if (!empty($this->data)) {
		
			$this->data['Categoria']['subcat_id']= $this->data['Categoria']['categorias'];

			$this->Categoria->create();
			if ($this->Categoria->save($this->data)) {
				$this->Session->setFlash(__('Se guardo la categoria', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('No se pudo guardar la categoria intentelo de nuevo.', true));
			}
		}
		$categorias = $this->Categoria->find('list', array('fields'=>array('nombre'),'conditions'=>array('subcat_id'=>'0'),'order'=>'id ASC') );
		$this->set('categorias', $categorias);
	}

//==========================================//
	function admin_edit($id = null) {
	if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
	$this->layout = "admin";
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Categoria', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
		$this->data['Categoria']['subcat_id']= $this->data['Categoria']['categorias'];
			if ($this->Categoria->save($this->data)) {
				$this->Session->setFlash(__('Se guardo la categoria', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('No se pudo guardar la categoria intentelo de nuevo.', true));
			}
		}
		if (empty($this->data)) {
			
			$this->data = $this->Categoria->read(null, $id);
		}
		$categorias = $this->Categoria->find('list',array('fields'=>array('nombre'),'conditions'=>array('subcat_id'=>'0'),'order'=>'id ASC'));
		$this->set(compact('categorias'));
	}

//==========================================//
	function admin_delete($id = null) {
	if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Categoria', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Categoria->del($id)) {
			$this->Session->setFlash(__('Categoria deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>
